package com.dmcb.trade.business.crawlers.article;

import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

/**
 * 乐居网文章解析
 * Created by wangyong on 2017/7/11.
 */
@Service
public class LeJuParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.LEJU_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h1.title";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "p.origin>span";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.article-body";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.article-body>p img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "span.name>a,div.name";
    }

    @Override
    protected void setAuthorName(Document doc, Article article) {
        String authorName =  doc.select(dateSelect()).last().text();
        authorName = authorName.replace("作者：","").replace("来源：","").replace(":","").replace("：","").trim();
        article.setAuthorName(authorName);
    }

    /**
     * 日期是否在末尾元素
     *
     * @return 默认否
     */
    @Override
    protected boolean isDateLast() {
        return false;
    }

    @Override
    protected boolean isProxyEnabled() {
        return true;
    }

    @Override
    protected String platform() {
        return "乐居";
    }
}
